<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \interop\Container\ContainerInterface;
use Top\schema\factory\SchemaReader;
use Top\schema\field\ComplexField;
use Top\schema\value\ComplexValue;
use Top\schema\field\InputField;
use Top\schema\factory\SchemaWriter;
class  ItemController
{
	protected $app;

    public function __construct(ContainerInterface $ci)
    {
        $this->app = $ci;
    }

    public function writeXml(Request $request, Response $response, $args){
		$schemaReader = new SchemaReader();

		$b = $schemaReader->readXmlForMap(file_get_contents(dirname(__dir__).'/../vendor/taobao/item.xml'));

		print_r($b['description']->getFieldList());exit;
		$b['title']->setValue('接口测试');
		$b['item_type']->setValue('b');
		$b['auction_point']->setValue('0.5');
		$b['price']->setValue('129.00');
		$b['delivery_way']->addValues('2');

		$InputField = new InputField();
		$Complexfield = new ComplexField();
		$complexValue = new ComplexValue();
		
		//所在地
	    $prov = $b['location']->getField('prov');
	    $city = $b['location']->getField('city');
	    $prov->setValue('浙江');
	    $city->setValue('杭州');
	    $complexValue->put($prov);
	    $complexValue->put($city);
	    $b['location']->setComplexValue($complexValue);
	    // print_r($b['location']);exit;

	    //sku
	    $complexValue = new ComplexValue();
	    $sku1 = $b['sku']->getField('prop_1627207');
	    $sku2 = $b['sku']->getField('sku_price');
	    $sku3 = $b['sku']->getField('sku_quantity');
	    $sku4 = $b['sku']->getField('sku_outerId');
	    $complexValue->put($sku1);
	    $complexValue->put($sku2);
	    $complexValue->put($sku3);
	    $complexValue->put($sku4);
	    $b['sku']->addComplexValues($complexValue);

	    //商品描述
	    $complexValue = new ComplexValue();
	    $desc1 = $b['description']->getField('desc_module_50_cat_mod');
	    $desc = $b['description']->getField('desc_module_user_mods');
	  	$complexValue->put($desc1);
	  	$complexValue->put($desc);
	  	// $complexValue->put($desc2);
	  	// print_r($complexValue->getValue('prov','city'));  	
	  	$b['description']->setComplexValue($complexValue);

		$complexValue = new ComplexValue();
	  	$desc2 = $b['description']->getField('desc_module_50_cat_mod')->getField('desc_module_50_cat_mod_content');
	  	$desc3 = $b['description']->getField('desc_module_50_cat_mod')->getField('desc_module_50_cat_mod_order');
	  	$desc2->setValue('测试信息测试信息测试信息');
	  	$desc3->setValue('2');
	  	$complexValue->put($desc2);
	  	$complexValue->put($desc3);
		$b['description']->getField('desc_module_50_cat_mod')->setComplexValue($complexValue);

	  	
	  	$complexValue = new ComplexValue();
	  	$desc4 = $b['description']->getField('desc_module_user_mods')->getField('desc_module_user_mod_name');
	  	$desc5 = $b['description']->getField('desc_module_user_mods')->getField('desc_module_user_mod_content');
	  	$desc6 = $b['description']->getField('desc_module_user_mods')->getField('desc_module_user_mod_order');
	  	$desc4->setValue('测试');
	  	$desc5->setValue('测试信息测试信息测试信息');
	  	$desc6->setValue('3');
	  	$complexValue->put($desc4);
	  	$complexValue->put($desc5);
	  	$complexValue->put($desc6);
	  	$b['description']->getField('desc_module_user_mods')->addComplexValues($complexValue);

		$b['has_invoice']->setValue('true');
		$b['service_version']->setValue('11100');
		$b['item_status']->setValue('2');
		$b['quantity']->setValue('200');
		

		$schemaWriter = new SchemaWriter();
		$c = $schemaWriter->writeParamXml($b,'./return.xml');
		exit;
		// print_r($c);exit;

		set_time_limit(0);

	    include "TopSdk.php";

	    date_default_timezone_set('Asia/Shanghai'); 

	    $c = new TopClient;
	    $c->appkey = '23038146';
	    $c->secretKey = '4dd6883954417a28bfea022777c488a9';
	    $sessionKey = '61002190f216567c65757a3d445186b1a0d6b9caadd0ea0352469034';

	    $req = new TmallBrandcatControlGetRequest;
		$resp = $c->execute($req, $sessionKey);

		$req = new TmallItemSchemaAddRequest;
		$req->setCategoryId("50014075");
		$req->setProductId("842415233");
		$xml = file_get_contents('return.xml');
		$req->setXmlData($xml);
		$resp = $c->execute($req, $sessionKey);
		print_r($resp);
	}
}

